package com.gandong8.trace.tool.config.mysql;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.gandong8.trace.tool.config.GlobalProperties;
import com.gandong8.trace.tool.exception.GlobalException;
import com.gandong8.trace.tool.config.mysql.properties.DruidProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

//@Configuration
@EnableTransactionManagement
@EnableConfigurationProperties({DruidProperties.class, GlobalProperties.class})
//不扫描mapper会报错：org.apache.ibatis.binding.BindingException: Invalid bound statement
@MapperScan(basePackages = "com.gandong8.trace.dao.mapper")
@Slf4j
public class DruidConf {
    @Autowired
    DruidProperties myProps;
    @Autowired
    GlobalProperties globalProperties;


    @Bean("druidDataSource")
    public DataSource dataSource() {
        if(!globalProperties.getDruidOpen()) {
            System.err.println("druid datasource not allowed");
            return null;
        }
        if (myProps == null) {
            throw new GlobalException("DruidProperties is null");
        }
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(myProps.getDriverClassName());
        dataSource.setUrl(myProps.getUrl());
        dataSource.setUsername(myProps.getUsername());
        dataSource.setPassword(myProps.getPassword());
        log.info("-----druid datasource start!-----");
        return dataSource;
    }

    //若存在多个数据源需要配置多个SqlSessionFactory时可以使用@Primary指定，
    //若要做读写分离，可以在各自mybatis配置类中扫描不同的mapper包即可 也可以使用dertermineCurrentLookupKey做动态切换
    @Bean("druidSqlSessionFactory")
    @DependsOn({"druidDataSource","mybatisConfiguration"})
    public SqlSessionFactory sqlSessionFactory(@Qualifier("druidDataSource") DataSource dataSource,
                                               @Qualifier("mybatisConfiguration") MybatisConfiguration mybatisConfiguration
    ) throws Exception {
        if(!globalProperties.getDruidOpen()) {
            return null;
        }
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setConfiguration(mybatisConfiguration);
        return factoryBean.getObject();
    }


    @Bean(name = "druidTransactionManager")
    public DataSourceTransactionManager mallTransactionManager(@Qualifier("druidDataSource") DataSource dataSource) {
        if(!globalProperties.getDruidOpen()) {
            return null;
        }
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "druidSqlSessionTemplate")
    public SqlSessionTemplate mallSqlSessionTemplate(@Qualifier("druidSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        if(!globalProperties.getDruidOpen()) {
            return null;
        }
        return new SqlSessionTemplate(sqlSessionFactory);
    }




}
